package com.xuecheng.teaching.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.commons.constants.CommonConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.WorkChaDto;
import com.xuecheng.commons.model.dto.WorkDto;
import com.xuecheng.commons.model.vo.PageResponseResult;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.WorkVo;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.teaching.domain.CourseWorkRec;
import com.xuecheng.teaching.domain.Work;
import com.xuecheng.teaching.mapper.WorkMapper;
import com.xuecheng.teaching.service.CourseWorkRecService;
import com.xuecheng.teaching.service.WorkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 作业 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-08-13
 */
@Service
public class WorkServiceImpl extends ServiceImpl<WorkMapper, Work> implements WorkService {

    @Autowired
    private CourseWorkRecService courseWorkRecService;


    //分页条件查询课程计划作业列表
    @Override
    public ResponseResult findByPage(PageRequest request , WorkChaDto dto ) {
        //开启分页
        Page<Work> page = new Page<>(request.getPageNo(), request.getPageSize());
        //构架条件
        LambdaQueryWrapper<Work> qw = new LambdaQueryWrapper<>();
        //判断参数
        if (dto==null){
            return getResponseResult(page,qw);
        }

        qw.like(StrUtil.isNotBlank(dto.getTitle()),Work::getTitle,dto.getTitle());

        return getResponseResult(page,qw);

    }

    //  返回分页对象
    private  ResponseResult getResponseResult(Page<Work> page,LambdaQueryWrapper<Work> qw){
        page = this.page(page,qw);
        List<Work> workList = page.getRecords();
        //  封装
        List<WorkVo> voList = BeanHelper.copyWithCollection(workList, WorkVo.class);
        //  遍历，补充字段
        for (WorkVo workVo : voList) {
            workVo.setWorkId(workVo.getId());
            CourseWorkRec courseWorkRec = courseWorkRecService.getById(workVo.getWorkId());
            //  判断课程作业是否存在提交记录
            if (ObjectUtil.isNotEmpty(courseWorkRec)){  //  存在，封装答题人数
                workVo.setUserNum(courseWorkRec.getAnswerNumber());
            }else{  //  不存在，答题人数默认0
                workVo.setUserNum(0);
            }
        }
        return PageResponseResult.okResult(page.getTotal(),voList);
    }

    //新增或修改作业
    @Override
    public ResponseResult saveOrUpdateWork(WorkDto dto) {

        //  校验
        if (dto.getTitle() == null || dto.getQuestion() == null) {
            throw new BusinessException(ErrorCode.PARAMSERROR);//参数错误
        }
        //  dto转 实体
        Work work = BeanHelper.copyProperties(dto, Work.class);
        work.setId(dto.getWorkId());
        if (work.getId() == null){
            work.setCreateDate(DateUtil.date());
        }
        work.setChangeDate(DateUtil.date());
        this.saveOrUpdate(work);
        return ResponseResult.okResult();
    }

    //  删除课程计划的作业
    @Override
    public ResponseResult deleteById(Long workId) {

        // 删除数据库
        boolean isSuccess = this.removeById(workId);
        if (!isSuccess) {
            throw new BusinessException(ErrorCode.ERROR);
        }
        // 返回结果
        return ResponseResult.okResult();

    }

    //  根据id查询课程计划作业
    @Override
    public ResponseResult findById(Long workId) {
        // 根据作业id查询数据库
        Work work = this.getById(workId);

        WorkVo workVo = BeanHelper.copyProperties(work,WorkVo.class);
        workVo.setWorkId(workId);
        return ResponseResult.okResult(workVo);
    }

}
